package com.ifengchao.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

/**
 * 常用公共方法
 * */
public class CommonUtil {
	private static final Logger logger = Logger.getLogger(CommonUtil.class);
	
	/**
	 * 将异常堆栈的信息转成String
	 * @param t
	 * @return
	 */
	public static String printStackTraceToString(Throwable t) {
	    StringWriter sw = new StringWriter();
	    t.printStackTrace(new PrintWriter(sw, true));
	    return sw.getBuffer().toString();
	}

		
		
		/**
		 * 返回组合好的参数拼串(参数值经过URLEncoder.encode转码)
		 */
		public static String getParams(Map<String,Object> paramsMap){	
			
			LogUtil.getStart("CommonUtil.getParams", "开始拼装参数方法", "CommonUtil", "");
			
			StringBuffer buffer = new StringBuffer();
			
			boolean flag = false;
			
				try {
					
					for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
						
						String key = entry.getKey();
						String value = String.valueOf(entry.getValue());
						
					   System.out.println("key= "+ key + " and value= " + value);
					   
					   if(flag)
						   buffer.append("&");
					   
					   buffer.append(key);
					   buffer.append("=");
					   
					   if(StringUtil.isNotEmpty(value))
					   		value = URLEncoder.encode(value, "UTF-8");
					   
					   buffer.append(value);
					  
					   flag = true;
					   
					 }
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				LogUtil.getEnd("CommonUtil.getParams", "结束拼装参数方法", "CommonUtil");
				
			return buffer.toString();
			
		}
		
		/**
		 * 返回组合好的参数拼串
		 */
		public static String getParamsMap(Map<String,Object> paramsMap){	
			
			LogUtil.getStart("CommonUtil.getParamsMap", "开始拼装参数方法", "CommonUtil", "");
			
			StringBuffer buffer = new StringBuffer();
			
			boolean flag = false;
			
				try {
					
					for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
						
						String key = entry.getKey();
						String value = String.valueOf(entry.getValue());
						
					 logger.info("------------------------------------CommonUtil getParamsMap() key=" + key + " , value=" + value + "-----------------------------------");
					   
					   if(flag)
						   buffer.append("&");
					   
					   buffer.append(key);
					   buffer.append("=");
					   buffer.append(value);
					  
					   flag = true;
					 }
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				LogUtil.getEnd("CommonUtil.getParamsMap", "结束拼装参数方法", "CommonUtil");
				
			return buffer.toString();
			
		}
		
	
	 /** 
     * 将json格式的字符串解析成Map对象 <li> 
     * json格式：{"name":"admin","retries":"3fff","testname" 
     * :"ddd","testretries":"fffffffff"} 
     */  
	public static HashMap<String, Object> jsonObjToHashMap(Object object) { 
        HashMap<String, Object> data = new HashMap<String, Object>();  
        // 将json字符串转换成jsonObject  
        JSONObject jsonObject = JSONObject.fromObject(object);  
        Iterator it = jsonObject.keys();  
        // 遍历jsonObject数据，添加到Map对象  
        while (it.hasNext())  
        {  
            String key = String.valueOf(it.next());  
            Object value = jsonObject.get(key);  
            data.put(key, value);  
        }  
        return data;  
    }  

}
